<?php

namespace app\modules\Finance\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\modules\Finance\models\Invoice;

/**
 * InvoiceSearch represents the model behind the search form about `app\modules\Finance\models\Invoice`.
 */
class InvoiceSearch extends Invoice
{

    public $product_id;   // 团期编号
    public $order_code;     // 订单编号
    public $cruise_id;   // 邮轮编号
    public $min_price;
    public $max_price; 
    public $sort; //排序

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'finance_id', 'status', 'user_id'], 'integer'],
            [['code', 'name', 'company', 'tax', 'bank_account', 'bank_name', 'address', 'phone', 'remark', 'product_id','order_code','cruise_id','created_at', 'updated_at','min_price','max_price','sort'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Invoice::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $query->joinWith(['invoiceOrder']);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        //  审批项显示范围权限验证
        if(!Yii::$app->user->can('is_finance') && !Yii::$app->user->can('is_admin')){
            $this->user_id = Yii::$app->user->identity->id; 
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'finance_id' => $this->finance_id,
            'status' => $this->status,
            'user_id' => $this->user_id,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,

            // 关联
            'invoice_order.product_id' => $this->product_id,
            'invoice_order.order_code'   => $this->order_code,
        ]);

        $query->andFilterWhere(['like', 'code', $this->code])
            ->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'company', $this->company])
            ->andFilterWhere(['like', 'tax', $this->tax])
            ->andFilterWhere(['like', 'bank_account', $this->bank_account])
            ->andFilterWhere(['like', 'bank_name', $this->bank_name])
            ->andFilterWhere(['like', 'address', $this->address])
            ->andFilterWhere(['like', 'phone', $this->phone])
            ->andFilterWhere(['like', 'remark', $this->remark]);

        return $dataProvider;
    }

    public function search_all($params)
    {
        $query = Invoice::find();

        // add conditions that should always apply here

        // $dataProvider = new ActiveDataProvider([
        //     'query' => $query,
        // ]);

        $query->joinWith(['invoiceOrder']);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        //  审批项显示范围权限验证
        if(!Yii::$app->user->can('is_finance') && !Yii::$app->user->can('is_admin')){
            $this->user_id = Yii::$app->user->identity->id; 
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'finance_id' => $this->finance_id,
            'status' => $this->status,
            'user_id' => $this->user_id,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,

            // 关联
            'invoice_order.product_id' => $this->product_id,
            'invoice_order.order_code'   => $this->order_code,
        ]);

        $query->andFilterWhere(['like', 'code', $this->code])
            ->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'company', $this->company])
            ->andFilterWhere(['like', 'tax', $this->tax])
            ->andFilterWhere(['like', 'bank_account', $this->bank_account])
            ->andFilterWhere(['like', 'bank_name', $this->bank_name])
            ->andFilterWhere(['like', 'address', $this->address])
            ->andFilterWhere(['like', 'phone', $this->phone])
            ->andFilterWhere(['like', 'remark', $this->remark]);

        return $query->all();
    }


}
